package com.softme.trade.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Transient;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
public class User implements Serializable, UserDetails
{
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id_user")
	private Long id;

	private String login;

	private String password;

	private boolean active = true;

	private String name;

	private String email;

	@ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "user_firm",
       joinColumns = { @JoinColumn(name = "id_user") },
       inverseJoinColumns = { @JoinColumn(name = "id_firm") })
	private Set<Firm> firms = new HashSet<Firm>();

	@ManyToOne(cascade = CascadeType.ALL)
	@JoinColumn(name="id_role")  
	private Role role;

	private Language language;

	public User()
	{
		role = new Role();
		firms = new HashSet<Firm>();
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getEmail()
	{
		return email;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	@Transient
	public Collection<GrantedAuthority> getAuthorities()
	{
		List<GrantedAuthority> lista = new ArrayList<GrantedAuthority>();

		//TODO Apenas para testes
		lista.add(this.getRole());

		return lista;
	}

	@Transient
	public String getPassword()
	{
		return this.password;
	}

	@Transient
	public String getUsername()
	{
		return this.login;
	}

	@Transient
	public boolean isAccountNonExpired()
	{
		return this.active;
	}

	@Transient
	public boolean isAccountNonLocked()
	{
		return this.active;
	}

	@Transient
	public boolean isCredentialsNonExpired()
	{
		return this.active;
	}

	@Transient
	public boolean isEnabled()
	{
		return this.active;
	}

	public Long getId()
	{
		return id;
	}

	public void setId(Long id)
	{
		this.id = id;
	}

	public boolean isActive()
	{
		return active;
	}

	public void setActive(boolean active)
	{
		this.active = active;
	}

	public String getLogin()
	{
		return login;
	}

	public void setLogin(String login)
	{
		this.login = login;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public Role getRole()
	{
		return role;
	}

	public void setRole(Role role)
	{
		this.role = role;
	}

	public Language getLanguage()
	{
		return language;
	}

	public void setLanguage(Language language)
	{
		this.language = language;
	}

	public Set<Firm> getFirms()
	{
		return firms;
	}
	
	public void setFirms(Set<Firm> firms)
	{
		this.firms = firms;
	}
	
	/**
	 * Retorna o locale a partir da linguagem selecionada por usuário
	 * 
	 * @return
	 */
	@Transient
	public Locale getLocale()
	{
		return new Locale(this.language.getLanguage());
	}
}